import sys
sys.path.append("../../")

from model.StockBaseInfo import *
from model.CommonDb import *
import datetime

class HyMoneyDayController:
    def __init__(self,args):
        self.args = args
        self.service = StockBaseInfo()
        self.today = datetime.datetime.now().strftime("%Y-%m-%d")
    def run(self):
        if self.args["func"] == "getHyMoneyDay":
            return self.getHyMoneyDay(self.args)
        elif self.args["func"] == "getCurrentHyMoneyDay":
            return self.getCurrentHyMoneyDay(self.args)
        elif self.args["func"] == "getJlrDay":
            return self.getJlrDay(self.args)
        elif self.args["func"] == "getCurrentJlrDay":
            return self.getCurrentJlrDay(self.args)
        elif self.args["func"] == "getZsInfoDay":
            return self.getZsInfoDay(self.args)
        elif self.args["func"] == "getCurrentZsInfoDay":
            return self.getCurrentZsInfoDay(self.args)

    """
    获取每日行业的净收入
    """
    def getHyMoneyDay(self,args):
        db = CommonDb("money_per_day","sto")
        hy_money_data = self.service.getDfcfHyMoney()
        for row in hy_money_data:
            info = {
                "code": row["f12"],
                "count_day": self.today,
                "codename": row["f14"],
                "flow_money": row["f62"]
            }
            db.add(info)

    def getCurrentHyMoneyDay(self,args):
        hy_money_data = self.service.getDfcfHyMoney()
        for row in hy_money_data:
            info = {
                "code": row["f12"],
                "count_day": self.today,
                "codename": row["f14"],
                "flow_money": row["f62"]
            }
            print(info)

    """
    获取每日指数的信息
    """
    def getZsInfoDay(self,args):
        db = CommonDb("zs_info_day","sto")
        hy_money_data = self.service.getDfcfZsInfo()
        for row in hy_money_data:
            info = {
                "code": row["f12"],
                "count_day": self.today,
                "price": row["f2"],
                "up_price": row["f4"],
                "zf": row["f3"],
                "money": row["f6"],
                "up": row["f104"],
                "equal": row["f106"],
                "down": row["f105"]
            }
            db.add(info)

    def getCurrentZsInfoDay(self,args):
        hy_money_data = self.service.getDfcfZsInfo()
        for row in hy_money_data:
            info = {
                "code": row["f12"],
                "count_day": self.today,
                "price": row["f2"],
                "up_price": row["f4"],
                "zf": row["f3"],
                "money": row["f6"],
                "up": row["f104"],
                "equal": row["f105"],
                "down": row["f106"]
            }
            print(info)

    def getJlrDay(self,args):
        db = CommonDb("jlr_day","sto")
        jlr_data = self.service.getJingliuru(args["code"])
        current = []
        for row in jlr_data["klines"]:
            current = row.split(",")
        info = {
            "code": jlr_data["code"],
            "codename": jlr_data["name"],
            "count_day": self.today,
            "zhuli": round(float(current[1])/100000000,2),
            "chaodadan": round(float(current[5])/100000000,2),
            "dadan": round(float(current[4])/100000000,2),
            "zhongdan": round(float(current[3])/100000000,2),
            "xiaodan": round(float(current[2])/100000000,2)
        }
        db.add(info)

    def getCurrentJlrDay(self,args):
        jlr_data = self.service.getJingliuru(str(args["code"]))
        current = []
        for row in jlr_data["klines"]:
            current = row.split(",")
        info = {
            "code": jlr_data["code"],
            "codename": jlr_data["name"],
            "count_day": self.today,
            "zhuli": current[1],
            "chaodadan": current[5],
            "dadan": current[4],
            "zhongdan": current[3],
            "xiaodan": current[2]
        }
        print(info)
